Don't panic when printing the precise source id
authorAlex Crichton <alex@alexcrichton.com>
Sat, 31 Oct 2015 17:03:18 +0000 (10:03 -0700)
committerAlex Crichton <alex@alexcrichton.com>
Sat, 31 Oct 2015 17:03:18 +0000 (10:03 -0700)
Closes #2094

src/cargo/core/source.rs
tests/test_cargo_compile_git_deps.rs

index d258b9f160f0a8ff79342a8a1b593d0c7ce35bb6..02f30533b5f3fb63409b5a173bfe6862ed7f9629 100644 (file)
@@ -1,4 +1,4 @@
-use std::cmp::Ordering;
+use std::cmp::{self, Ordering};
 use std::collections::hash_map::{HashMap, Values, IterMut};
 use std::fmt::{self, Formatter};
 use std::hash;
@@ -294,14 +294,12 @@ impl fmt::Display for SourceId {
                             ref precise, .. } => {
                 try!(write!(f, "{}{}", url, url_ref(reference)));
 
-                match *precise {
-                    Some(ref s) => {
-                        try!(write!(f, "#{}", &s[..8]));
-                    }
-                    None => {}
+                if let Some(ref s) = *precise {
+                    let len = cmp::min(s.len(), 8);
+                    try!(write!(f, "#{}", &s[..len]));
                 }
                 Ok(())
-            },
+            }
             SourceIdInner { kind: Kind::Registry, ref url, .. } => {
                 write!(f, "registry {}", url)
             }
index fcab5d846a33162811b60e9858c1971bb56e6bef..e85853546b537d6c76f69e5d9721e5444256ad60 100644 (file)
@@ -647,6 +647,17 @@ test!(update_with_shared_deps {
                  .arg("-p").arg("dep1"),
                 execs().with_stdout(""));
 
+    // Don't do anything bad on a weird --precise argument
+    println!("bar bad precise update");
+    assert_that(p.cargo("update")
+                 .arg("-p").arg("bar")
+                 .arg("--precise").arg("0.1.2"),
+                execs().with_status(101).with_stderr("\
+Unable to update [..]
+
+To learn more, run the command again with --verbose.
+"));
+
     // Specifying a precise rev to the old rev shouldn't actually update
     // anything because we already have the rev in the db.
     println!("bar precise update");